package bluerain.io.adapter.ui;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import bluerain.io.adapter.R;

/**
 * Created by foredawn on 15-9-13.
 * GridView基本用法
 */
public class GridVIewActivity extends Activity {

    GridView gridView;
    ImageView imageView;

    private int[] images = new int[]{
            R.drawable.xiao_ming,
            R.drawable.li_bai,
            R.drawable.wang_cai,
            R.drawable.xiao_ming,
            R.drawable.li_bai,
            R.drawable.wang_cai,
            R.drawable.xiao_ming,
            R.drawable.li_bai,
            R.drawable.wang_cai,
            R.drawable.xiao_ming,
            R.drawable.li_bai,
            R.drawable.wang_cai
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_view_activity);
        gridView = (GridView) findViewById(R.id.grid_view);
        imageView = (ImageView) findViewById(R.id.img_source);

        List<Map<String, Object>> items = new ArrayList<>();
        Map<String, Object> item = null;
        for (int i = 0; i < images.length; i++) {
            item = new HashMap<>();
            item.put("image", images[i]);
            items.add(item);
        }
        SimpleAdapter adapter = new SimpleAdapter(this, items,
                R.layout.grid_item,
                new String[]{"image"},
                new int[]{R.id.grid_item_img});
        gridView.setAdapter(adapter);

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                imageView.setImageResource(images[position]);
            }
        });

    }
}
